/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "str_分割.h"

#include <boost/algorithm/string.hpp>


std::vector<std::wstring> f_str_分割(const std::wstring& 源, const std::wstring& 分隔符)
{
	std::vector<std::wstring> 分割后文本;
	boost::algorithm::split(分割后文本, 源, boost::algorithm::is_any_of(分隔符));
	//boost::algorithm::erase_all(tmpStrErase, "Hello");
	for (std::vector<std::wstring>::iterator i = 分割后文本.begin(); i != 分割后文本.end(); ) {
		if ((*i).size()) ++i;
		else i = 分割后文本.erase(i);
	}

	return 分割后文本;
}

void f_str_删除两头字符(std::wstring& 源, const std::wstring& 删除符)
{
	源.erase(0, 源.find_first_not_of(删除符));
	源.erase(源.find_last_not_of(删除符) + 1);
}

void f_str_删除两头字符(std::wstring& 源, const std::wstring& 头, const std::wstring& 尾)
{
	源.erase(0, 源.find_first_not_of(头));
	源.erase(源.find_last_not_of(尾) + 1);
}

ivec2 f_str_查找到字符区间(std::wstring& 源, const std::wstring& 查找符, const std::wstring& 终止符)
{
	ivec2 r;
	auto posA = 源.find(查找符);
	auto posB = 源.find(终止符);

	if (posA != std::wstring::npos) {
		if (posB > posA) {
			r.x = posA;
		}
		else {
			r.x = -1;
		}
	}
	else {
		r.x = -1;
	}

	if (posA != std::wstring::npos) {
		r.y = posB;
	}
	else {
		r.y = -1;
	}
	
	return r;
}

std::wstring f_str_取到字符(std::wstring& 源, const std::wstring& 终止符)
{
	auto posA = 源.find(终止符);
	std::wstring r;

	if (posA != std::wstring::npos) {
		r = 源.substr(0, posA);
		//源.erase(posA + 终止符.size());
	}

	return r;
}

wchar_t f_str_从跳过字符取字符(std::wstring& 源, const std::wstring& 跳过字符)
{
	for (auto& e : 源) {
		bool 是否跳过 = false;
		for (auto k : 跳过字符) {
			if (k == e) {
				是否跳过 = true;
				break;
			}
		}

		if (是否跳过) {
			continue;
		}

		return e;
	}
	return 0;
}

void f_str_去除空白字符(std::wstring& s) {
	uint32 num = s.size();
	for (uint32 i = 0; i < num; ) {
		if (i >= s.size()) break;
		if (s[i] == ' ') {
			s.erase(i, 1);
		}
		else {
			++i;
		}
	}
}

bool f_str_去路径空白字符(std::wstring& path) {
	uint32 num = path.size();
	for (uint32 i = 0; i < num; ) {
		if (i >= path.size()) break;
		if (path[i] == ' ') {
			path.erase(i, 1);
		}
		else {
			++i;
		}
	}
	return false;
}




